/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.builder;

import java.util.Dictionary;
import java.util.Hashtable;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSlider;

public class JSliderBuilder extends AbstractJComponentBuilder<JSliderBuilder, JSlider>
{

    private Integer minimum;
    private Integer maximum;
    private Integer majorTickSpacing;
    private Integer minorTickSpacing;
    private Boolean paintTicks;
    private Boolean paintLabels;
    private Boolean paintTrack;
    private Boolean snapToTicks;
    private Integer value;

    private Dictionary<Integer, JComponent> labels;

    public JSliderBuilder()
    {
        super(JSlider.class);
    }

    public JSliderBuilder minimum(int minimum)
    {
        this.minimum = Integer.valueOf(minimum);

        return this;
    }

    public JSliderBuilder maximum(int maximum)
    {
        this.maximum = Integer.valueOf(maximum);

        return this;
    }

    public JSliderBuilder range(int minimum, int maximum)
    {
        return minimum(minimum).maximum(maximum);
    }

    public JSliderBuilder majorTickSpacing(int majorTickSpacing)
    {
        this.majorTickSpacing = Integer.valueOf(majorTickSpacing);

        return paintTicks(true);
    }

    public JSliderBuilder minorTickSpacing(int minorTickSpacing)
    {
        this.minorTickSpacing = Integer.valueOf(minorTickSpacing);

        return this;
    }

    public JSliderBuilder paintTicks(boolean paintTicks)
    {
        this.paintTicks = Boolean.valueOf(paintTicks);

        return this;
    }

    public JSliderBuilder ticks(int majorTickSpacing, int minorTickSpacing, boolean paintTicks)
    {
        return majorTickSpacing(majorTickSpacing).minorTickSpacing(minorTickSpacing).paintTicks(paintTicks);
    }

    public JSliderBuilder paintLabels(boolean paintLabels)
    {
        this.paintLabels = Boolean.valueOf(paintLabels);

        return this;
    }

    public JSliderBuilder paintTrack(boolean paintTrack)
    {
        this.paintTrack = Boolean.valueOf(paintTrack);

        return this;
    }

    public JSliderBuilder snapToTicks(boolean snapToTicks)
    {
        this.snapToTicks = Boolean.valueOf(snapToTicks);

        return this;
    }

    public JSliderBuilder value(int value)
    {
        this.value = Integer.valueOf(value);

        return this;
    }

    public JSliderBuilder label(int value, String label)
    {
        return label(value, new JLabel(label));
    }

    public JSliderBuilder label(int value, JComponent component)
    {
        if (labels == null)
        {
            labels = new Hashtable<Integer, JComponent>();
        }

        labels.put(Integer.valueOf(value), component);
        return paintLabels(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void populate(JSlider component)
    {
        super.populate(component);

        if (minimum != null)
        {
            component.setMinimum(minimum.intValue());
        }

        if (maximum != null)
        {
            component.setMaximum(maximum.intValue());
        }

        if (majorTickSpacing != null)
        {
            component.setMajorTickSpacing(majorTickSpacing.intValue());
        }

        if (minorTickSpacing != null)
        {
            component.setMinorTickSpacing(minorTickSpacing.intValue());
        }

        if (paintTicks != null)
        {
            component.setPaintTicks(paintTicks.booleanValue());
        }

        if (paintLabels != null)
        {
            component.setPaintLabels(paintLabels.booleanValue());
        }

        if (paintTrack != null)
        {
            component.setPaintTrack(paintTrack.booleanValue());
        }

        if (snapToTicks != null)
        {
            component.setSnapToTicks(snapToTicks.booleanValue());
        }
        
        if (value != null)
        {
            component.setValue(value.intValue());
        }

        if (labels != null)
        {
            component.setLabelTable(labels);
        }
    }
}
